home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio / Ham Radio CD-ROM (Emerald Software) (1995).ISO / misc / 9q920411 / eagle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  24.8 KB  |  831 lines

  1. /*
  2.  * Interface driver for the EAGLE board for KA9Q's TCP/IP on an IBM-PC ONLY!
  3.  *
  4.  *  Written by Art Goldman, WA3CVG - (c) Copyright 1987 All Rights Reserved
  5.  *  Permission for non-commercial use is hereby granted provided this notice
  6.  *  is retained.  For info call: (301) 997-3838.
  7.  *
  8.  *  10 Jan 88    ng6q    - Corrected IDLE comparison in doegstat.
  9.  *   6 Apr 88    ng6q    - Changed eg_raw to prevent calling egtxint with a
  10.  *              packet in sndbuf.  Initialized sndq and rcvq in
  11.  *              eg_attach.  Added carrier detect check before
  12.  *              slot time delay in egtxint.  Should make major
  13.  *              changes to egtxint to avoid delay loops while
  14.  *              masked for receive interrupts.
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <dos.h>
  19. #include "global.h"
  20. #include "mbuf.h"
  21. #include "iface.h"
  22. #include "pktdrvr.h"
  23. #include "netuser.h"
  24. #include "eagle.h"
  25. #include "8530.h"
  26. #include "ax25.h"
  27. #include "trace.h"
  28. #include "pc.h"
  29. #include "devparam.h"
  30. #include <time.h>
  31.  
  32. static int32 eg_ctl __ARGS((struct iface *iface,int cmd,int set,int32 val));
  33. static int eg_raw __ARGS((struct iface *iface,struct mbuf *bp));
  34. static int eg_stop __ARGS((struct iface *iface));
  35. static void egchanparam __ARGS((struct egchan *hp));
  36. static void egexint __ARGS((struct egchan *hp));
  37. static void egrxint __ARGS((struct egchan *hp));
  38. static void egtxint __ARGS((struct egchan *hp));
  39. static void rts __ARGS((struct egchan *hp,int16 x));
  40. static void waitmsec __ARGS((int n));
  41.  
  42. static struct EGTAB Eagle[EGMAX];    /* Device table - one entry per card */
  43. static INTERRUPT (*eghandle[])() = {    /* handler interrupt vector table */
  44.     eg0vec,    
  45. };
  46. static struct egchan Egchan[2*EGMAX];    /* channel table - 2 entries per card */
  47. static int16 Egnbr;
  48.  
  49. /* Master interrupt handler.  One interrupt at a time is handled.
  50.  * here. Service routines are called from here.
  51.  */
  52. INTERRUPT (far *(egint)(dev))()
  53. int dev;
  54. {
  55.     register char st;
  56.     register int16 pcbase;
  57.     struct egchan *hp;
  58.     struct EGTAB *ep;
  59.  
  60.     ep = &Eagle[dev];
  61.     ep->ints++;
  62.     pcbase = ep->addr;
  63.  
  64.     /* Read interrupt status register from channel A */
  65.     while((st = read_scc(pcbase+CHANA+CTL,R3)) != 0) {
  66.         /* Use IFs to process ALL interrupts pending
  67.          * because we need to check all interrupt conditions
  68.          */
  69.         if (st & CHARxIP) {
  70.             /* Channel A Rcv Interrupt Pending */
  71.             hp = &Egchan[2 * dev];
  72.             egrxint(hp);
  73.         } else if (st & CHATxIP) {
  74.             /* Channel A Transmit Int Pending */
  75.             hp = &Egchan[2 * dev];
  76.             egtxint(hp);
  77.         } else if (st & CHAEXT) {
  78.             /* Channel A External Status Int */
  79.             hp = &Egchan[2 * dev];
  80.             egexint(hp);
  81.         } else if (st & CHBRxIP) {
  82.             /* Channel B Rcv Interrupt Pending */
  83.             hp = &Egchan[(2 * dev)+1];
  84.             egrxint(hp);
  85.         } else if (st & CHBTxIP) {
  86.             /* Channel B Transmit Int Pending */
  87.             hp = &Egchan[(2 * dev)+1];
  88.             egtxint(hp);
  89.         } else if (st & CHBEXT) {
  90.             /* Channel B External Status Int */
  91.             hp = &Egchan[(2 * dev)+1];
  92.             egexint(hp);
  93.         }
  94.         /* Reset highest interrupt under service */
  95.         write_scc(hp->base+CTL,R0,RES_H_IUS);
  96.     } /* End of while loop on int processing */
  97.     return ep->chain ? ep->oldvec : NULL;
  98. }
  99.  
  100. /* Eagle SIO External/Status interrupts
  101.  * This can be caused by a receiver abort, or a Tx UNDERRUN/EOM.
  102.  * Receiver automatically goes to Hunt on an abort.
  103.  *
  104.  * If the Tx Underrun interrupt hits, change state and
  105.  * issue a reset command for it, and return.
  106.  */
  107. static void
  108. egexint(hp)
  109. register struct egchan *hp;
  110. {
  111.     char st, i_state;
  112.  
  113.     i_state = dirps();        /* disable interrupts */
  114.     hp->exints++;
  115.     st = read_scc(hp->base+CTL,R0);     /* Fetch status */
  116.  
  117.     /* Check for Tx UNDERRUN/EOM - only in Transmit Mode */
  118.     if((hp->rstate==0) && (st & TxEOM)) {
  119.         /* if in UNDERRUN, go to FLAGOUT state
  120.          * see explanation under egtxint()
  121.          * CRC & FLAG now going out, so
  122.          * wait for Tx BUffer Empty int
  123.          */
  124.  
  125.         /* If we are not in underrun, this is an unexpected
  126.          * underrun.  EOM bit should be set, so the SCC will
  127.          * now send an abort
  128.          */
  129.  
  130.         if(hp->tstate == UNDERRUN)
  131.             hp->tstate = FLAGOUT;
  132.  
  133.         /* Tx Buff EMPTY interrupt occurs after CRC is sent */
  134.     }
  135.  
  136.     /* Receive Mode only
  137.      * This triggers when hunt mode is entered, & since an ABORT
  138.      * automatically enters hunt mode, we use that to clean up
  139.      * any waiting garbage
  140.      */
  141.     if((hp->rstate == ACTIVE) && (st & BRK_ABRT)) {
  142.         hp->rcp = hp->rcvbuf->data;
  143.         hp->rcvbuf->cnt = 0;          /* rewind on DCD transition */
  144.         hp->aborts++;              /* nbr aborts * 2 */
  145.     }
  146.  
  147.     /* reset external status latch */
  148.     write_scc(CTL+hp->base,R0,RES_EXT_INT);
  149.  
  150.     restore(i_state);
  151. }
  152.  
  153. /* EG receive interrupt handler. The first receive buffer is pre-allocated
  154.  * in the init routine.  Thereafter, it is filled here, queued out, and a
  155.  * new one acquired.  CRC, OVERRUN and TOOBIG errors merely 'rewind' the
  156.  * pointers and reuse the same buffer.
  157.  */
  158. static void
  159. egrxint(hp)
  160. register struct egchan *hp;
  161. {
  162.     register int16 base;
  163.     char rse, i_state;
  164.  
  165.     i_state = dirps();        /* disable interrupts */
  166.     hp->rxints++;
  167.     base = hp->base;
  168.  
  169.     if ((read_scc(base+CTL,R0)) & Rx_CH_AV) {
  170.         /* there is a char to be stored
  171.          * read special condition bits before reading the data char
  172.          */
  173.         rse = read_scc(hp->base+CTL,R1); /* get status byte from R1 */
  174.         if(rse & Rx_OVR) {
  175.             /* Rx overrun - toss buffer */
  176.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  177.             hp->rcvbuf->cnt = 0;
  178.             hp->rstate = RXERROR;    /* set error flag */
  179.             hp->rovers++;        /* count overruns */
  180.         } else if(hp->rcvbuf->cnt >= hp->bufsiz) {
  181.             /* Too large -- toss buffer */
  182.             hp->toobig++;
  183.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  184.             hp->rcvbuf->cnt = 0;
  185.             hp->rstate = TOOBIG;    /* when set, chars are not stored */
  186.         }
  187.         /* ok, we can store the received character now */
  188.         if(hp->rstate == ACTIVE) {        /* If no errors... */
  189.             *hp->rcp++ = inportb(base+DATA);    /* char to rcv buff */
  190.             hp->rcvbuf->cnt++;            /* bump count */
  191.         } else {
  192.             /* got to empty FIFO */
  193.             (void) inportb(base+DATA);
  194.             write_scc(hp->base+CTL,R0,ERR_RES);    /* reset err latch */
  195.             hp->rstate = ACTIVE;
  196.         }
  197.     }
  198.     /* char has been stored
  199.      * read special condition bits
  200.      */
  201.     rse = read_scc(hp->base+CTL,R1);     /* get status byte from R1 */
  202.  
  203.     /* The End of Frame bit is ALWAYS associated with a character,
  204.      * usually, it is the last CRC char.  Only when EOF is true can
  205.      * we look at the CRC byte to see if we have a valid frame
  206.      */
  207.     if(rse & END_FR) {
  208.         hp->rxframes++;
  209.         /* END OF FRAME -- Make sure Rx was active */
  210.         if(hp->rcvbuf->cnt > 0) {        /* any data to store */
  211.             /* looks like a frame was received
  212.              * now is the only time we can check for CRC error
  213.              */
  214.             if((rse & CRC_ERR) || (hp->rstate > ACTIVE) || (hp->rcvbuf->cnt < 10)) {
  215.                 /* error occurred; toss frame */
  216.                 if(rse & CRC_ERR)
  217.                     hp->crcerr++;    /* count CRC errs */
  218.                 if(hp->rstate == RXERROR)
  219.                     hp->rovers++;
  220.                 /* don't throw away buffer -
  221.                  * merely reset the pointers
  222.                  */
  223.                 hp->rcp = hp->rcvbuf->data;
  224.                 hp->rcvbuf->cnt = 0;
  225.             } else {
  226.                 /* Here we have a valid frame */
  227.                 hp->rcvbuf->cnt -= 2;           /* Toss 2 crc bytes */
  228.                 net_route(hp->iface,CL_AX25,hp->rcvbuf);
  229.                 /* packet queued - get buffer for next frame */
  230.                 hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  231.                 hp->rcp = hp->rcvbuf->data;
  232.                 hp->rcvbuf->cnt = 0;
  233.                 if(hp->rcvbuf == NULLBUF) {
  234.                     /* No memory, abort receiver */
  235.                     restore(i_state);
  236.                     tprintf("DISASTER! Out of Memory for Receive!\n");
  237.                     write_scc(CTL+base,R3,Rx8);
  238.                     return;
  239.                 }
  240.             } /* end good frame queued */
  241.         }  /* end check for active receive upon EOF */
  242.         hp->rstate = ACTIVE;    /* and clear error status */
  243.     } /* end EOF check */
  244.     restore(i_state);
  245. }
  246.  
  247. /* egchan transmit interrupt service routine
  248.  *
  249.  * The state variable tstate, along with some static pointers,
  250.  * represents the state of the transmit "process".
  251.  */
  252. static void
  253. egtxint(hp)
  254. register struct egchan *hp;
  255. {
  256.     register int16 base;
  257.     char i_state;
  258.     int c;
  259.  
  260.     i_state = dirps();
  261.  
  262.     if(hp->tstate != DEFER && hp->tstate)
  263.         hp->txints++;
  264.     base = hp->base;
  265.  
  266.     switch(hp->tstate) {
  267.     case FLAGOUT:
  268.         /* Here after CRC sent and Tx interrupt fires.
  269.          * To give the SCC a chance to get the FLAG
  270.          * out, we delay 100 Ms
  271.          */
  272.         hp->tstate = IDLE;    /* fall thru to IDLE */
  273.         waitmsec(10);        /* 100 msec wait for flag Tx */
  274.         /* Note, it may be possible to stuff out a
  275.          * meaningless character, wait for the interrupt
  276.          * then go to idle.  A delay is absolutely necessary
  277.          * here else the packet gets truncated prematurely
  278.          * when no other packet is waiting to be sent.
  279.          * IDLE state indicates either we are starting a brand new packet
  280.          * as a result of its being queued for transmission (egtxint called
  281.          * from eg_raw), or after a frame has been transmitted (as a
  282.          * result of a Tx buffer empty interrupt after the CRC/FLAG
  283.          */
  284.     case IDLE:
  285.         /* Transmitter idle. Find a frame for transmission */
  286.         if((hp->sndbuf = dequeue(&hp->sndq)) == NULLBUF) {
  287.             /* Nothing to send - return to receive mode
  288.              * Tx OFF now - flag should have gone
  289.              */
  290.             rts(hp,OFF);
  291.             restore(i_state);
  292.             return;
  293.         }
  294.         /* If a buffer to send, we drop thru here */
  295.     case DEFER:        /* we may have deferred prev xmit attempt */
  296.         /* PPERSIST CALCULATION: we use the lower byte of the
  297.          * 8253 timer 0 count, as a random number (0-255).
  298.          * If the persist value is higher, wait one slot time
  299.          */
  300.         if(hp->persist >= peekb(0x40,0x6c))
  301.             waitmsec(hp->slotime);
  302.  
  303.         /* Check DCD so we don't step on a frame being received */
  304.         /* DCD is ACTIVE LOW on the SCC DCD pin, but the bit in R0 */
  305.         /* is SET when DCD is ACTIVE!! */
  306.  
  307.         if((read_scc(base+CTL,R0) & DCD) > 0) { /* Carrier Detected? */
  308.             hp->tstate = DEFER;    /* defer xmit */
  309.             /* don't release dequeued buffer...*/
  310.             restore(i_state);
  311.             return;
  312.         }
  313.  
  314.         rts(hp,ON);   /* Transmitter on */
  315.         /* ints not enabled yet */
  316.  
  317.         /* Get next char to send */
  318.         c = PULLCHAR(&hp->sndbuf);        /* one char at a time */
  319.         write_scc(CTL+base,R0,RES_Tx_CRC);    /* reset for next frame */
  320.         outportb(base+DATA,c);        /* First char out now */
  321.  
  322.         /* select transmit interrupts to enable */
  323.  
  324.         write_scc(CTL+base,R15,TxUIE);        /* allow Underrun int only */
  325.         write_scc(CTL+base,R1,TxINT_ENAB|EXT_INT_ENAB);  /* Tx/Extern ints on */
  326.         write_scc(CTL+base,R9,MIE|NV);        /* master enable */
  327.         /* enable interrupt latch on board */
  328.         outportb(hp->dmactrl,INTENABLE);
  329.  
  330.         hp->tstate = ACTIVE;    /* char going out now */
  331.         restore(i_state);
  332.         return;
  333.  
  334.     case ACTIVE:
  335.         /* Here we are actively sending a frame */
  336.         if((c = PULLCHAR(&hp->sndbuf)) != -1){
  337.             outportb(hp->base+DATA,c);    /* next char is gone */
  338.             /* stuffing a char satisfies Interrupt condition */
  339.         } else {
  340.             /* No more to send - just stop till underrun int */
  341.             hp->tstate = UNDERRUN;
  342.             free_p(hp->sndbuf);
  343.             /* now we reset the EOM latch & enable underrun int */
  344.             write_scc(CTL+base,R0,RES_EOM_L);    /* send CRC at underrun */
  345.             write_scc(CTL+hp->base,R0,RES_Tx_P); /* reset Tx Int Pend */
  346.         }
  347.         restore(i_state);
  348.         return;     /* back to wait for interrupt */
  349.  
  350.     case UNDERRUN:
  351.         /*
  352.          * This state is handled by an UNDERRUN interrupt, which
  353.          * is an External Status interrupt.  At UNDERRUN, the
  354.          * UNDERRUN/EOM latch in R0 will be 0, so the SCC will send
  355.          * CRC and ending flag.  After the CRC clears the Tx buffer,
  356.          * a TX BUFF EMPTY interrupt will fire.  At that time, we
  357.          * should be in FLAGOUT state, ready to send another frame
  358.          * if one is there to send.
  359.          */
  360.         break;
  361.     } /* end switch */
  362.     restore(i_state);
  363. }
  364.  
  365. /* SET Transmit or Receive Mode
  366.  * Set RTS (request-to-send) to modem on Transmit
  367.  */
  368. static void
  369. rts(hp,x)
  370. register struct egchan *hp;
  371. int16 x;
  372. {
  373. int16 tc;
  374. long br;
  375.  
  376.     /* Reprogram BRG and turn on transmitter to send flags */
  377.     if(x == ON) {                /* Turn Tx ON and Receive OFF */
  378.         write_scc(CTL+hp->base,R3,Rx8);    /* Rx off */
  379.         waitmsec(50);            /* 500 msec delay before on */
  380.         hp->rstate = IDLE;
  381.         write_scc(CTL+hp->base,R9,0);    /* Interrupts off */
  382.         br = hp->speed;         /* get desired speed */
  383.         tc = (XTAL/br)-2;        /* calc 1X BRG divisor */
  384.         write_scc(CTL+hp->base,R12,tc&0xFF);      /* lower byte */
  385.         write_scc(CTL+hp->base,R13,(tc>>8)&0xFF); /* upper bite */
  386.  
  387.         write_scc(CTL+hp->base,R5,TxCRC_ENAB|RTS|TxENAB|Tx8|DTR);
  388.         /* Transmitter now on */
  389.         write_scc(CTL+hp->base,R0,RES_Tx_CRC);/* CRC reset */
  390.         waitmsec(hp->txdelay);      /* Delay after Tx on */
  391.     } else {    /* Tx OFF and Rx ON */
  392.         hp->tstate = IDLE;
  393.         write_scc(CTL+hp->base,R5,Tx8|DTR);     /* TX off now */
  394.         write_scc(CTL+hp->base,R0,ERR_RES);     /* reset error bits */
  395.  
  396.         write_scc(CTL+hp->base,R1,(INT_ALL_Rx|EXT_INT_ENAB));
  397.         write_scc(CTL+hp->base,R15,BRKIE);        /* allow ABORT int */
  398.  
  399.         /* delay for squelch tail before enable of Rx */
  400.         waitmsec(hp->squeldelay);    /* keep it up  */
  401.  
  402.         /* Reprogram BRG for 32x clock for receive DPLL */
  403.         write_scc(CTL+hp->base,R14,BRSRC);         /* BRG off, but keep Pclk source */
  404.         br = hp->speed;             /* get desired speed */
  405.         tc = ((XTAL/32)/br)-2;            /* calc 32X BRG divisor */
  406.         write_scc(CTL+hp->base,R12,tc&0xFF);    /* lower byte */
  407.         write_scc(CTL+hp->base,R13,(tc>>8)&0xFF);    /* upper bite */
  408.         write_scc(CTL+hp->base,R14,BRSRC|SEARCH);    /* SEARCH mode, keep BRG source */
  409.         write_scc(CTL+hp->base,R14,BRSRC|BRENABL);    /* Enable the BRG */
  410.  
  411.         /* Now, turn on the receiver and hunt for a flag */
  412.         write_scc(CTL+hp->base,R3,RxENABLE|RxCRC_ENAB|Rx8);
  413.         hp->rstate = ACTIVE;            /* Normal state */
  414.     }
  415. }
  416.  
  417. /* Initialize eg controller parameters */
  418. static void
  419. egchanparam(hp)
  420. register struct egchan *hp;
  421. {
  422.     int16 tc;
  423.     long br;
  424.     char i_state;
  425.     register int16 base;
  426.  
  427.     /* Initialize 8530 channel for SDLC operation */
  428.  
  429.     base = hp->base;
  430. #ifdef    notdef
  431.     tprintf("Initializing Channel %c - Base = %x\n",base&2?'A':'B',base);
  432. #endif
  433.     i_state = dirps();
  434.  
  435.     switch(base & 2){
  436.     case 2:
  437.         write_scc(CTL+base,R9,CHRA);    /* Reset channel A */
  438.         break;
  439.     case 0:
  440.         write_scc(CTL+base,R9,CHRB);    /* Reset channel B */
  441.         break;
  442.     }
  443.  
  444.     /* Deselect all Rx and Tx interrupts */
  445.     write_scc(CTL+base,R1,0);
  446.  
  447.     /* Turn off external interrupts (like CTS/CD) */
  448.     write_scc(CTL+base,R15,0);
  449.  
  450.     /* X1 clock, SDLC mode */
  451.     write_scc(CTL+base,R4,SDLC|X1CLK);           /* SDLC mode and X1 clock */
  452.  
  453.     /* Now some misc Tx/Rx parameters */
  454.     /* CRC PRESET 1, NRZI Mode */
  455.     write_scc(CTL+base,R10,CRCPS|NRZI);
  456.  
  457.     /* Set up BRG and DPLL multiplexers */
  458.     /* Tx Clk from BRG. Rcv Clk from DPLL, TRxC pin outputs DPLL */
  459.     write_scc(CTL+base,R11,TCBR|RCDPLL|TRxCDP|TRxCOI);
  460.  
  461.     /* Null out SDLC start address */
  462.     write_scc(CTL+base,R6,0);
  463.  
  464.     /* SDLC flag */
  465.     write_scc(CTL+base,R7,FLAG);
  466.  
  467.     /* Set up the Transmitter but don't enable it */
  468.     /*  DTR, 8 bit TX chars only - TX NOT ENABLED */
  469.     write_scc(CTL+base,R5,Tx8|DTR);
  470.  
  471.     /* Receiver - intial setup only - more later */
  472.     write_scc(CTL+base,R3,Rx8);            /* 8 bits/char */
  473.  
  474.     /* Setting up BRG now - turn it off first */
  475.     write_scc(CTL+hp->base,R14,BRSRC);         /* BRG off, but keep Pclk source */
  476.  
  477.     /* set the 32x time constant for the BRG in Receive mode */
  478.  
  479.     br = hp->speed;             /* get desired speed */
  480.     tc = ((XTAL/32)/br)-2;            /* calc 32X BRG divisor */
  481.  
  482.     write_scc(CTL+hp->base,R12,tc&0xFF);      /* lower byte */
  483.     write_scc(CTL+hp->base,R13,(tc>>8)&0xFF); /* upper bite */
  484.  
  485.     /* Time to set up clock control register for RECEIVE mode
  486.      * Eagle has xtal osc going to pclk at 3.6864 Mhz
  487.      * The BRG is sourced from that, and set to 32x clock
  488.      * The DPLL is sourced from the BRG, and feeds the TRxC pin
  489.      * Transmit clock & Receive clock come from DPLL
  490.      */
  491.  
  492.     /* Following subroutine sets up and ENABLES the receiver */
  493.     rts(hp,OFF);           /* TX OFF and RX ON */
  494.  
  495.     write_scc(CTL+hp->base,R14,BRSRC|SSBR);       /* DPLL from BRG, BRG source is PCLK */
  496.     write_scc(CTL+hp->base,R14,BRSRC|SEARCH);       /* SEARCH mode, keep BRG source */
  497.  
  498.     write_scc(CTL+hp->base,R14,BRSRC|BRENABL);    /* Enable the BRG */
  499.  
  500.     /* enable the receive interrupts */
  501.  
  502.     write_scc(CTL+hp->base,R1,(INT_ALL_Rx|EXT_INT_ENAB));
  503.     write_scc(CTL+hp->base,R15,BRKIE);        /* ABORT int */
  504.     write_scc(CTL+hp->base,R9,MIE|NV);    /* master enable */
  505.  
  506.     /* enable interrupt latch on board */
  507.     outportb(hp->dmactrl,INTENABLE);
  508.  
  509.     /* Now, turn on the receiver and hunt for a flag */
  510.     write_scc(CTL+hp->base,R3,RxENABLE|RxCRC_ENAB|Rx8);
  511.  
  512.     restore(i_state);
  513. }
  514.  
  515. /* Attach a EAGLE interface to the system
  516.  * argv[0]: hardware type, must be "eagle"
  517.  * argv[1]: I/O address, e.g., "0x300"
  518.  * argv[2]: vector, e.g., "2"
  519.  * argv[3]: mode, must be:
  520.  *        "ax25" (AX.25 UI frame format)
  521.  * argv[4]: interface label, e.g., "eg0"
  522.  * argv[5]: receiver packet buffer size in bytes
  523.  * argv[6]: maximum transmission unit, bytes
  524.  * argv[7]: interface speed, e.g, "1200"
  525.  * argv[8]: First IP address, optional (defaults to Ip_addr);
  526.  * argv[9]: Second IP address, optional (defaults to Ip_addr);
  527.  */
  528. int
  529. eg_attach(argc,argv,p)
  530. int argc;
  531. char *argv[];
  532. void *p;
  533. {
  534.     register struct iface *if_pca,*if_pcb;
  535.     struct egchan *hp;
  536.     int dev;
  537.  
  538.     /* Quick check to make sure args are good and mycall is set */
  539.     if(strcmp(argv[3],"ax25") != 0){
  540.         tprintf("Mode %s unknown for interface %s\n",
  541.             argv[3],argv[4]);
  542.         return -1;
  543.     }
  544.     if(if_lookup(argv[4]) != NULLIF){
  545.         tprintf("Interface %s already exists\n",argv[4]);
  546.         return -1;
  547.     }
  548.     if(Mycall[0] == '\0'){
  549.         tprintf("set mycall first\n");
  550.         return -1;
  551.     }
  552.     /* Note: More than one card can be supported if you give up a COM:
  553.      * port, thus freeing up an IRQ line and port address
  554.      */
  555.  
  556.     if(Egnbr >= EGMAX) {
  557.         tprintf("Only 1 EAGLE controller supported right now!\n");
  558.         return -1;
  559.     }
  560.     dev = Egnbr++;
  561.  
  562.     /* Initialize hardware-level control structure */
  563.     Eagle[dev].addr = htoi(argv[1]);
  564.     Eagle[dev].vec = atoi(argv[2]);
  565.     if(strchr(argv[2],'c') != NULLCHAR)
  566.         Eagle[dev].chain = 1;
  567.     else
  568.         Eagle[dev].chain = 0;
  569.  
  570.  
  571.     /* Save original interrupt vector */
  572.     Eagle[dev].oldvec = getirq(Eagle[dev].vec);
  573.  
  574.     /* Set new interrupt vector */
  575.     if(setirq(Eagle[dev].vec,eghandle[dev]) == -1){
  576.         tprintf("IRQ %u out of range\n",Eagle[dev].vec);
  577.         Egnbr--;
  578.         return -1;
  579.     }
  580.     /* Create interface structures and fill in details */
  581.     if_pca = (struct iface *)callocw(1,sizeof(struct iface));
  582.     if_pcb = (struct iface *)callocw(1,sizeof(struct iface));
  583.  
  584.     if_pca->addr = if_pcb->addr = Ip_addr;
  585.     if(argc > 8)
  586.         if_pca->addr = resolve(argv[8]);
  587.     if(argc > 9)
  588.         if_pcb->addr = resolve(argv[9]);
  589.  
  590.     if(if_pca->addr == 0 || if_pcb->addr == 0){
  591.         tprintf(Noipaddr);
  592.         free((char *)if_pca);
  593.         free((char *)if_pcb);
  594.         return -1;
  595.     }
  596.     /* Append "a" to interface associated with A channel */
  597.     if_pca->name = mallocw((unsigned)strlen(argv[4])+2);
  598.     strcpy(if_pca->name,argv[4]);
  599.     strcat(if_pca->name,"a");
  600.     /* Append "b" to iface associated with B channel */
  601.     if_pcb->name = mallocw((unsigned)strlen(argv[4])+2);
  602.     strcpy(if_pcb->name,argv[4]);
  603.     strcat(if_pcb->name,"b");
  604.  
  605.     if_pcb->mtu = if_pca->mtu = atoi(argv[6]);
  606.     if_pcb->type = if_pca->type = CL_AX25;
  607.     if_pcb->ioctl = if_pca->ioctl = eg_ctl;
  608.     if_pca->dev = 2*dev;            /* eg0a */
  609.     if_pcb->dev = 2*dev + 1;        /* eg0b */
  610.     if_pcb->stop = if_pca->stop = eg_stop;
  611.     if_pcb->output = if_pca->output = ax_output;
  612.     if_pcb->raw = if_pca->raw = eg_raw;
  613.  
  614.     if(strcmp(argv[3],"ax25") == 0) {
  615.         /* Must be true, was checked at top */
  616.         if_pcb->send = if_pca->send = ax_send;
  617.         if(if_pcb->hwaddr == NULLCHAR)
  618.             if_pcb->hwaddr = mallocw(AXALEN);
  619.         memcpy(if_pcb->hwaddr,Mycall,AXALEN);
  620.         if(if_pca->hwaddr == NULLCHAR)
  621.             if_pca->hwaddr = mallocw(AXALEN);
  622.         memcpy(if_pca->hwaddr,Mycall,AXALEN);
  623.     }
  624.     /* Link em in to the interface chain */
  625.     if_pca->next = if_pcb;
  626.     if_pcb->next = Ifaces;
  627.     Ifaces = if_pca;
  628.  
  629.     /* set params in egchan table for CHANNEL B */
  630.  
  631.     hp = &Egchan[2*dev+1];                /* eg1 is offset 1 */
  632.     hp->iface = if_pcb;
  633.     hp->stata = Eagle[dev].addr + CHANA + CTL;    /* permanent status */
  634.     hp->statb = Eagle[dev].addr + CHANB + CTL;    /* addrs for CHANA/B*/
  635.     hp->dmactrl = Eagle[dev].addr + DMACTRL;    /* Eagle control reg */
  636.     hp->speed = (int16)atoi(argv[7]);
  637.     hp->base = Eagle[dev].addr + CHANB;
  638.     hp->bufsiz = atoi(argv[5]);
  639.     hp->tstate = IDLE;
  640.     /* default KISS Params */
  641.     hp->txdelay = 25;        /* 250 Ms */
  642.     hp->persist = 64;        /* 25% persistence */
  643.     hp->slotime = 10;        /* 100 Ms */
  644.     hp->squeldelay = 20;        /* 200 Ms */
  645.  
  646.     write_scc(CTL+hp->stata,R9,FHWRES);     /* Hardware reset */
  647.                         /* one time only */
  648.     /* Disable interrupts with Master interrupt ctrl reg */
  649.     write_scc(CTL+hp->stata,R9,0);
  650.  
  651.     egchanparam(hp);
  652.  
  653.     /* Pre-allocate a receive buffer */
  654.     hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  655.     if(hp->rcvbuf == NULLBUF) {
  656.         /* No memory, abort receiver */
  657.         tprintf("EGATTACH: No memory available for Receive buffers\n");
  658.         /* Restore original interrupt vector */
  659.         setirq(Eagle[dev].vec,Eagle[dev].oldvec);
  660.         Egnbr--;
  661.         return(-1);
  662.     }
  663.     hp->rcp = hp->rcvbuf->data;
  664.     hp->rcvbuf->cnt = 0;
  665.     hp->sndq = NULLBUF;
  666.  
  667.     /* set params in egchan table for CHANNEL A */
  668.     hp = &Egchan[2*dev];            /* eg0a is offset 0 */
  669.     hp->iface = if_pca;
  670.     hp->speed = (int16)atoi(argv[7]);
  671.     hp->base = Eagle[dev].addr + CHANA;
  672.     hp->bufsiz = atoi(argv[5]);
  673.     hp->tstate = IDLE;
  674.     /* default KISS Params */
  675.     hp->txdelay = 25;        /* 250 Ms */
  676.     hp->persist = 64;        /* 25% persistence */
  677.     hp->slotime = 10;        /* 100 Ms */
  678.     hp->squeldelay = 20;        /* 200 Ms */
  679.  
  680.     egchanparam(hp);
  681.  
  682.     /* Pre-allocate a receive buffer */
  683.     hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  684.     if(hp->rcvbuf == NULLBUF) {
  685.         /* No memory, abort receiver */
  686.         tprintf("EGATTACH: No memory available for Receive buffers\n");
  687.         /* Restore original interrupt vector */
  688.         setirq(Eagle[dev].vec,Eagle[dev].oldvec);
  689.         Egnbr--;
  690.         return -1;
  691.     }
  692.     hp->rcp = hp->rcvbuf->data;
  693.     hp->rcvbuf->cnt = 0;
  694.     hp->sndq = NULLBUF;
  695.  
  696.     write_scc(CTL+hp->base,R9,MIE|NV);        /* master interrupt enable */
  697.  
  698.     /* Enable interrupts on the EAGLE card itself */
  699.     outportb(hp->dmactrl,INTENABLE);
  700.  
  701.     /* Enable interrupt */
  702.     maskon(Eagle[dev].vec);
  703.  
  704.     if_pca->txproc = newproc("eagle tx",512,if_tx,0,if_pca,NULL,0);
  705.     if_pcb->txproc = newproc("eagle tx",512,if_tx,0,if_pcb,NULL,0);
  706.     
  707.     return 0;
  708. }
  709.  
  710.  
  711. /* Shut down interface */
  712. static int
  713. eg_stop(iface)
  714. struct iface *iface;
  715. {
  716.     int dev;
  717.  
  718.     dev = iface->dev;
  719.     if(dev & 1)
  720.         return 0;
  721.     dev >>= 1;    /* Convert back into eagle number */
  722.  
  723.     /* Turn off interrupts */
  724.     maskoff(Eagle[dev].vec);
  725.  
  726.     /* Restore original interrupt vector */
  727.     setirq(Eagle[dev].vec,Eagle[dev].oldvec);
  728.  
  729.     /* Force hardware reset */
  730.     write_scc(CTL+Eagle[dev].addr + CHANA,R9,FHWRES);
  731.  
  732.     /* resets interrupt enable on eagle card itself */
  733.     outportb(Eagle[dev].addr+DMACTRL,0);
  734.     return 0;
  735. }
  736.  
  737. /* Send raw packet on eagle card */
  738. static int
  739. eg_raw(iface,bp)
  740. struct iface *iface;
  741. struct mbuf *bp;
  742. {
  743.     char kickflag;
  744.     struct egchan *hp;
  745.  
  746.     dump(iface,IF_TRACE_OUT,CL_AX25,bp);
  747.     iface->rawsndcnt++;
  748.     iface->lastsent = secclock();
  749.     hp = &Egchan[iface->dev];
  750.     kickflag = (hp->sndq == NULLBUF) & (hp->sndbuf == NULLBUF);    /* clever! flag=1 if something in queue */
  751.     enqueue(&hp->sndq,bp);
  752.     if(kickflag)            /* simulate interrupt to xmit */
  753.         egtxint(hp);        /* process interrupt */
  754.     return 0;
  755. }
  756. /* routine to delay n increments of 10 milliseconds
  757.  * about right on a turbo XT - will be slow on 4.77
  758.  * Still looking for a way to use the 8253 timer...
  759.  */
  760. static void
  761. waitmsec(n)
  762. int n;
  763. {
  764.     long i;
  765.  
  766.     for(i=0L; i < (200L*n); i++)
  767.         ;  /* simple loop delay */
  768. }
  769.  
  770. /* display EAGLE Channel stats */
  771. int
  772. doegstat(argc,argv,p)
  773. int argc;
  774. char *argv[];
  775. void *p;
  776. {
  777.     struct egchan *hp0, *hp1;
  778.     int i;
  779.  
  780.     for(i=0; i<EGMAX; i++) {
  781.         hp0 = &Egchan[i];
  782.         hp1 = &Egchan[i+1];
  783.  
  784.         tprintf("EAGLE Board Statistics:\n\n");
  785.         tprintf("Base Addr\tRxints\tTxints\tExints\tEnqued\tCrcerr\tAborts\tRxOvers\tRFrames\n");
  786.         tprintf("---------\t------\t------\t------\t------\t------\t------\t-------\t-------\n");
  787.         tprintf("0x%x\t\t%ld\t%ld\t%ld\t%d\t%d\t%d\t%d\t%d\nRcv State=%s\n", hp0->base, hp0->rxints,
  788.             hp0->txints, hp0->exints, hp0->enqueued, hp0->crcerr, hp0->aborts,
  789.             hp0->rovers,hp0->rxframes,
  790.             hp0->rstate==0?"IDLE":hp0->rstate==1?"ACTIVE":hp0->rstate==2?"RXERROR":hp0->rstate==3?"RXABORT":"TOOBIG");
  791.  
  792.         tprintf("0x%x\t\t%ld\t%ld\t%ld\t%d\t%d\t%d\t%d\t%d\nRcv State=%s\n\n", hp1->base, hp1->rxints,
  793.             hp1->txints, hp1->exints, hp1->enqueued, hp1->crcerr, hp1->aborts,
  794.             hp1->rovers,hp1->rxframes,
  795.             hp1->rstate==0?"IDLE":hp1->rstate==1?"ACTIVE":hp1->rstate==2?"RXERROR":hp1->rstate==3?"RXABORT":"TOOBIG");
  796.     }
  797.     return 0;
  798. }
  799.  
  800. /* Subroutine to set kiss params in channel tables */
  801. static int32
  802. eg_ctl(iface,cmd,set,val)
  803. struct iface *iface;
  804. int cmd;
  805. int set;
  806. int32 val;
  807. {
  808.     struct egchan *hp;
  809.  
  810.     hp = &Egchan[iface->dev];        /* point to channel table */
  811.     switch(cmd){
  812.     case PARAM_TXDELAY:
  813.         if(set)
  814.             hp->txdelay = val;
  815.         return hp->txdelay;
  816.     case PARAM_PERSIST:
  817.         if(set)
  818.             hp->persist = val;
  819.         return hp->persist;
  820.     case PARAM_SLOTTIME:
  821.         if(set)
  822.             hp->slotime = val;
  823.         return hp->slotime;
  824.     case PARAM_TXTAIL:
  825.         if(set)
  826.             hp->squeldelay = val;
  827.         return hp->squeldelay;
  828.     }
  829.     return -1;
  830. }
  831.